package zouran.controller;

import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import zouran.entity.News;
import zouran.service.NewsService;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;
import java.util.Map;

import com.alibaba.fastjson.JSON;

/**
 * @Author: zouran
 * @Description:
 * @Date: Created in 2022 2022/12/4 11:05
 **/
@RestController
@CrossOrigin
@RequestMapping("/news")
public class NewsController {
    private NewsService newsService;
    @Autowired
    public void setNewsService(NewsService newsService) {
        this.newsService = newsService;
    }

    /**
     * 根据标题查找文章
     * @param title 文章标题
     * @return News对象
     */
    @RequestMapping("/select_byTitle")
    public List<News> select_byTitle(@RequestParam String title)
    {
       return newsService.select_byTitle(title);
    }

    /**
     * 根据作者名字查询新闻
     * @param author 作者名字
     * @return News对象
     */
    @RequestMapping("/select_byAuthor")
    public List<News> select_byAuthor(@RequestParam String author)
    {
        return newsService.select_byAuthor(author);
    }

    /**
     * 获得所有的新闻信息
     * @return List集合
     */
    @RequestMapping("/getAllNews")
    public List<News> getAllNews(){
        return newsService.getAllNews();
    }

    /**
     * 前端传入string 类，作者名
     * @return boolean
     */
    @RequestMapping("/delete_news")
    public boolean  delete_news(@RequestBody News news){
        return newsService.delete_news(news);
    }
    /**
     * 发布新闻添加
     */
    @RequestMapping("/add_news")
    public boolean add_news(@RequestBody News news){
        return newsService.add_news(news);
    }
    /**
     * 更新新闻
     */
    @RequestMapping("/update_news")
    public boolean update_news(@RequestBody News news){return newsService.update_news(news);}
    /**
     * 条件模糊查找,layui数据接口json数据导致对象不能直接赋值
     * JSON数据处理
     */
    @RequestMapping("/select_byWrapper")
    public List<News> select_byWrapper(@RequestBody String news){
        JSONObject jsonObject = JSONObject.parseObject(news);
        JSONObject data=jsonObject.getJSONObject("news");
        News news1=JSON.toJavaObject(data,News.class);
        return newsService.select_byWrapper(news1);
    }

    /**
     * 文件上传接口
     * @param file 文件
     * @return 文件存储的相对地址
     */
    @RequestMapping("/upload_file")
    public Map<String, Object> upload_file(@RequestParam MultipartFile file){
        return newsService.upload_file(file);
    }
}
